<?php
namespace addons\weidun_card\model;

class model
{
    public $table;

    /**
     * 获取一条记录
     * @param array $params
     * @return bool
     */
    public function getOne($params = array())
    {
        global $_W;

        $params['uniacid'] = $_W['uniacid'];
        $res = pdo_get($this->table, $params);
        return $res;
    }

    /**
     * 获取多条记录
     * @param array $condition
     * @param array $fields
     * @param string $keyfield
     * @param array $orderby
     * @param array $limit
     * @return array
     */
    public function getAll($condition = array(), $fields = array('*'), $keyfield = '', $orderby = array(), $limit = array(0, 10))
    {
        global $_W;

        $condition['uniacid'] = $_W['uniacid'];
        $list = pdo_getall($this->table, $condition, $fields, $keyfield, $orderby, $limit);

        return $list;
    }

    /**
     * 获取分页记录
     * @param $page
     * @param string $where
     * @param array $params
     * @param int $psize
     * @param string $orderBy
     * @param string $keyField
     * @return array
     */
    public function getList($page, $where = "", $params = array(), $psize = 10, $orderBy = "id desc", $keyField = "id")
    {
        global $_W, $_GPC;
        if (empty($page)) $page = 1;
        $total = 0;
        $params['uniacid'] = $_W['uniacid'];
        $where .= " AND uniacid = :uniacid";
        $sql = "SELECT * FROM " . tablename($this->table) . " WHERE 1 {$where} ORDER BY {$orderBy} limit " . (($page - 1) * $psize) . "," . $psize;
        $result = array();
        $result['list'] = pdo_fetchall($sql, $params);
        $sql = "SELECT COUNT({$keyField}) FROM " . tablename($this->table) . " WHERE 1 {$where}";
        $total = pdo_fetchcolumn($sql, $params);

        $result['pager'] = pagination($total, $page, $psize);
        if (empty($result)) {
            return array();
        } else {
            return $result;
        }
    }

    /**
     * 通过id获取一条记录
     * @param $id
     * @return bool
     */
    public function getInfo($id)
    {
        global $_W;
        $item = pdo_get($this->table, array('id' => $id));
        return $item;
    }

    public function getTotal($params = array())
    {
        global $_W;
        $params['uniacid'] = $_W['uniacid'];
        $count = pdo_getcolumn($this->table, $params, "count(id)");
        return $count;
    }

    /**
     * 通过id删除一条记录
     * @param $id
     * @return bool
     */
    public function delete($id)
    {
        if (empty($id))  return false;
        pdo_delete($this->table, array('id' => $id));
    }

    /**
     * 通过参数删除记录
     * @param array $params
     */
    public function deleteByParams($params = array())
    {
        global $_W;
        $params['uniacid'] = $_W['uniacid'];
        pdo_delete($this->table, $params);
    }

    /**
     * 添加或者更新记录
     * @param $data
     * @return mixed
     */
    public function update($data)
    {
        global $_W;
        $data['uniacid'] = $_W['uniacid'];
        if (empty($data['id'])) {
            pdo_insert($this->table, $data);
            $data['id'] = pdo_insertid();
        } else {
            //更新
            pdo_update($this->table, $data, array('uniacid' => $_W['uniacid'], 'id' => $data['id']));
        }
        return $data;
    }
}